/* eslint-disable max-classes-per-file */
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-08-16 17:36:20
 * @LastEditors: swc
 * @LastEditTime: 2025-02-14 13:32:05
 */
import { N_oSetupPanel } from '../../types';
import { T_compPanelConf } from '../compConfPanel';
import { T_toolbarLib } from '../compToolbar';
import { T_compBase } from './compBase';

type I_tableStyle = N_oSetupPanel.N_comps.I_tableStyle;
type I_compToolbar = N_oSetupPanel.N_comps.I_compToolbar;
type I_messageConf = N_oSetupPanel.N_comps.I_messageConf;

export class T_refBaseGridConf {
  name: string;

  comment: string;

  isCreated: boolean;

  refTable: string; // 引用功能

  status: string;

  tableStyle: I_tableStyle;

  editWindow: boolean;

  tableGraphMode: boolean;

  dbType: N_oSetupPanel.N_comps.DB_TYPE;

  // 表格
  tableColumns: Array<any>;

  // 唯一索引
  uniqueIndexes: Array<any>;

  // 索引
  indexes: Array<any>;

  // 规则
  rules: Array<any>;

  // 选单
  selbill: Array<any>;

  // 选单配置
  queryLink: Array<any>;

  // 联查设置
  action: object;

  headerGroup: Array<any>;

  // 表头分组
  charts: Array<any>;

  // 图表设置
  toolbar: I_compToolbar;

  qbeSearch: object;

  bancodeScan: object;

  // 工具栏
  tableDataSetting: {
    editable: boolean;
    autoload: boolean; // 表数据处理
  };

  messageConfig: I_messageConf;

  triggerEvent: object;
}
export class T_refBaseGrid extends T_compBase {
  readonly compName = N_oSetupPanel.N_comps.COMP_NAME.REF_BASE_GRID;

  public conf: T_refBaseGridConf = new T_refBaseGridConf();

  private TtoolbarLib: T_toolbarLib = new T_toolbarLib();

  private TcompPanelConf: T_compPanelConf = new T_compPanelConf();

  constructor() {
    super();
    this.conf = {
      name: '',
      comment: '',
      isCreated: false,
      refTable: '', // 引用功能
      status: '',
      tableStyle: {
        showHeader: true,
        showFooter: true,
        showPager: true,
        showToolbar: true,
        border: false,
        mode: N_oSetupPanel.N_comps.TABLESTYLE_MODE.MODE_NULL
      }, // 表格样式
      editWindow: false,
      tableGraphMode: false,
      dbType: N_oSetupPanel.N_comps.DB_TYPE.MYSQL,
      tableColumns: [],
      uniqueIndexes: [], // 唯一索引
      indexes: [], // 索引
      rules: [], // 规则
      selbill: [], // 选单配置
      queryLink: [], // 联查设置
      action: {},
      headerGroup: [], // 表头分组
      charts: [], // 图表设置
      qbeSearch: {
        enable: false,
        initSpace: 5,
        isInitShow: false,
        position: N_oSetupPanel.N_comps.POSITION.POSITION_TOP
      },
      // 条码扫描
      bancodeScan: {
        enable: false,
        initSpace: 5,
        terminator: '',
        position: N_oSetupPanel.N_comps.POSITION.POSITION_TOP
      },
      toolbar: null, // 工具栏
      tableDataSetting: {
        editable: true,
        autoload: true
      }, // 表数据处理
      messageConfig: {
        custDefListener: {
          search: [],
          coordination: null
        },
        emitActions: {
          search: [],
          refresh: []
        },
        eventGroup: null
      },
      triggerEvent: { rowDbClick: false }
    };
    this.conf.toolbar = this.TtoolbarLib.getCompToolBarSet(
      N_oSetupPanel.N_comps.COMP_NAME.REF_BASE_GRID
    );
  }

  public addTableColumn(...p: any[]): void {
    this.conf.tableColumns.push(...p);
  }

  public addUniqueIndexes(...p: any[]): void {
    this.conf.uniqueIndexes.push(...p);
  }

  public addIndexes(...p: any[]): void {
    this.conf.indexes.push(...p);
  }

  public addRules(...p: any[]): void {
    this.conf.rules.push(...p);
  }

  public addSelbill(...p: any[]): void {
    this.conf.selbill.push(...p);
  }

  public addQueryLink(...p: any[]): void {
    this.conf.queryLink.push(...p);
  }

  public addHeaderGroup(...p: any[]): void {
    this.conf.headerGroup.push(...p);
  }

  public addCharts(...p: any[]): void {
    this.conf.charts.push(...p);
  }

  public getConf(): T_refBaseGridConf {
    return this.conf;
  }

  public getComponentId(): N_oSetupPanel.N_comps.COMP_MODULE_ID {
    return N_oSetupPanel.N_comps.COMP_MODULE_ID.BASE_GRID;
  }

  public GetCompPanelConf(): N_oSetupPanel.N_comps.I_setupPanel {
    return this.TcompPanelConf.getCompPanelSet(
      N_oSetupPanel.N_comps.COMP_NAME.REF_BASE_GRID
    );
  }
  /**
   * @description: 设置basegrid权限
   * @return {*}
   * @author: wiz
   * @Date: 2023-08-23 09:50:57
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */

  public getPermissionList(
    config: T_refBaseGridConf
  ): Array<N_oSetupPanel.I_permission> {
    return [
      {
        type: 'button',
        area: 'toolbar',
        data: this.getToolbarKey(config)
      },

      {
        type: 'selbill',
        area: 'toolbar',
        data: this.getSelbillData(config)
      },
      {
        type: 'customButton',
        area: 'toolbar',
        data: this.getPmsAddonData(config)
      },
      {
        type: 'columns',
        area: 'table',
        data: this.getPmsColumn(config)
      }
    ];
  }

  private getToolbarKey(config: T_refBaseGridConf): Array<string> {
    return Object.keys(this.conf.toolbar);
  }

  private getSelbillData(config: T_refBaseGridConf): Array<string> {
    return [];
  }

  private getPmsColumn(config: T_refBaseGridConf): Array<string> {
    const arr: Array<string> = [];
    config.tableColumns.forEach(col => {
      arr.push(col.field);
    });
    return arr;
  }

  private getPmsAddonData(config: T_refBaseGridConf): Array<string> {
    const arr: Array<string> = [];
    if (Array.isArray(config?.toolbar?.addon)) {
      config.toolbar.addon.forEach(ctm => {
        arr.push(ctm.title);
        if (ctm.children.length > 0) {
          // eslint-disable-next-line no-use-before-define
          handleAddonData(ctm.children);
        }
      });
    }

    function handleAddonData(children): void {
      for (const child of children) {
        arr.push(child.title);
        if (child.children.length > 0) {
          handleAddonData(child.children);
        }
      }
    }
    return arr;
  }
}
